Imports System.Net
Public Class FormLogin

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        getComputerIp()
    End Sub
    Private Sub LogInSystem()
        CmdPro.Parameters("@UserName") = txtUserName.Text
        CmdPro.Parameters("@Password") = Crypto.PasswordHash(txtUserName.Text, txtPassword.Text)

        Dim dt = CmdPro.ExecuteDataTable("SystemLogin")
        If dt.Rows.Count <> 1 Then
            MsgBox("Invalid user name or password", vbInformation)
            Exit Sub
        End If
        Dim row = dt.Rows(0)
        If row("Locked") = True Then
            MsgBox("User is already locked~!", vbInformation)
            Exit Sub
        End If

        App.UserName = row("UserName")
        App.UserFullName = row("FullName")
        App.UserId = row("UserId")
        CmdPro.Parameters("@UserId") = row("UserId")
        App.AuthorizedPermissions = CmdPro.ExecuteDataTable("SystemLoginWithPermission")
        If row("ChangePasswordAtLogOn") = True Then
            Dim frm = New FormChangePassword()
            frm.txtUserName.Text = txtUserName.Text
            frm.txtPassword.Text = txtPassword.Text
            frm.ShowDialog()
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
        PictureBox1.Hide()
        PictureBox2.Show()
        Dim user As New UserlogInfo
        user.UserId = UserId
        user.MyDate = Now.ToShortDateString
        user.Time = Now.ToShortTimeString
        user.Active = 1
        UserLog.InsertUserLog(user)
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        If MessageBox.Show("are you sure want to close ?", "close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Application.Exit()




    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Dbinterface.OpenConnection()
        initalizeData()
        ' Add any initialization after the InitializeComponent() call.

    End Sub
 

    Private Sub initalizeData()
        For Each obj As Object In PC_IPs.getIP.Values
            cboIp2.Items.Add(obj)
        Next
    End Sub

    Private Sub txtPassword_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPassword.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnLogin_Click(sender, e)
        End If

    End Sub

    Private Sub FormLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        cboIp1.Hide()
        cboIp2.Hide()
        'Me.Text = GetComputerName()

        cboIp1.Items.AddRange(Dns.GetHostAddresses(GetComputerName))



    End Sub
    Private Function GetComputerName() As String
        Return Dns.GetHostName
    End Function
    Private Sub getComputerIp()
        For Each ip1 In cboIp1.Items
            For Each ip2 In cboIp2.Items
                If ip1.ToString = ip2.ToString Then
                    LogInSystem()
                Else

                End If

            Next

        Next
    End Sub
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        btnLogin_Click(sender, e)
    End Sub
End Class